﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;

namespace CustomBA.HelpClass
{
	public sealed class RegeditHelper
	{
		private static RegeditHelper instance = null;
		private static readonly object padlock = new object();
		private RegeditHelper()
		{
		}
		public static RegeditHelper Instance
		{
			get
			{
				lock (padlock)
				{
					if (instance == null)
					{
						instance = new RegeditHelper();
					}
					return instance;
				}
			}
		}
		public string GetFirstUsefulDrive()
		{
			string result = "C:\\";

			DriveInfo[] drives = DriveInfo.GetDrives();

			foreach (DriveInfo drive in drives)
			{
				if (drive.IsReady && drive.Name != "C:\\" && (drive.DriveType == DriveType.Fixed || drive.DriveType == DriveType.Removable))
				{
					result = drive.Name;
					break;
				}
			}

			return result;

		}
		public string GetUninstallPackage()
		{
			try
			{
				using (RegistryKey pathKey = Registry.CurrentUser.OpenSubKey(RegKeyPath))
				{
					if (pathKey == null)
						return null;

					var strs = pathKey.GetValue("UninstallPackage");
					if (strs == null)
						return null;
					return strs.ToString();
				}
			}
			catch { }
			return null;
		}
		static string RegKeyPath = @"SOFTWARE\Microsoft\Jinzhangshu\IntoTheVoid";
		public string GetInstallPathReg()
		{
			try
			{
				using (RegistryKey pathKey = Registry.CurrentUser.OpenSubKey(RegKeyPath))
				{
					if(pathKey == null)
						return null;

					var strs = pathKey.GetValue("InstallPath");
					if (strs == null)
						return null;
					return strs.ToString();
				}
			}
			catch { }
			return null;
		}
		public void SetInstallPathReg(string path)
		{
			try
			{
				using (RegistryKey pathKey = Registry.CurrentUser.CreateSubKey(RegKeyPath))
				{
					pathKey.SetValue("InstallPath", path);
				}
			}
			catch { }
		}
		public string GetInstalledVersionReg()
		{
			try
			{
				using (RegistryKey pathKey = Registry.CurrentUser.OpenSubKey(RegKeyPath))
				{
					if (pathKey == null)
						return null;
					var strs = pathKey.GetValue("Version");
					if (strs == null)
						return null;
					return strs.ToString();
				}
			}
			catch { }
			return null;
		}
		public string GetGameInstallPathReg()
		{
			try
			{
				using (RegistryKey pathKey = Registry.CurrentUser.OpenSubKey(RegKeyPath))
				{
					if (pathKey == null)
						return null;
					var strs = pathKey.GetValue("GameInstallDir");
					if (strs == null)
						return null;
					return strs.ToString();
				}
			}
			catch { }
			return null;
		}
		public void RemoveVersionReg()
		{
			using (RegistryKey pathKey = Registry.CurrentUser.OpenSubKey(RegKeyPath, true))
			{
				if (pathKey == null)
					return;
				var strs = pathKey.GetValue("Version");
				if(strs != null)
				{
					path = strs.ToString();
					pathKey.DeleteValue("Version");
				}
			}
		}
		static string gamePath = "";
		static string path = "";

		public void RemoveReg()
		{
			do
			{
				using (RegistryKey pathKey = Registry.CurrentUser.OpenSubKey(RegKeyPath, true))
				{
					if (pathKey == null)
						break;
					var strs = pathKey.GetValue("InstallPath");
					path = strs.ToString();
					pathKey.DeleteValue("InstallPath");
				}
			} while (false);
			do
			{
				using (RegistryKey pathKey = Registry.CurrentUser.OpenSubKey(RegKeyPath, true))
				{
					if (pathKey == null)
						break;
					var strs = pathKey.GetValue("GameInstallDir");
					if(strs != null)
					{
						gamePath = strs.ToString();
						pathKey.DeleteValue("GameInstallDir");
					}

				}
			} while (false);
		}
		public void ResetReg()
		{
			do
			{
				using (RegistryKey pathKey = Registry.CurrentUser.OpenSubKey(RegKeyPath,true))
				{
					if (pathKey == null)
						break;
					pathKey.SetValue("InstallPath", path);
				}
			} while (false);
			do
			{
				using (RegistryKey pathKey = Registry.CurrentUser.OpenSubKey(RegKeyPath, true))
				{
					if (pathKey == null)
						break;
					pathKey.SetValue("GameInstallDir", gamePath);
				}
			} while (false);
		}
		public bool IsInstalled()
		{
			using (RegistryKey pathKey = Registry.CurrentUser.OpenSubKey(RegKeyPath, true))
			{
				if (pathKey == null)
					return false;
				try
				{
					var sValueNameColl = pathKey.GetValueNames();
					if (sValueNameColl.Length > 0)
					{
						return true;
					}
				}
				catch { }
				return false;
			}
		}
		public bool VersionIsNewer(string oldV, string newV)
		{
			if (oldV == null || oldV == "")
				return false;

			string[] parts1 = oldV.Split('.');
			string[] parts2 = newV.Split('.');

			int maxLength = Math.Max(parts1.Length, parts2.Length);

			for (int i = 0; i < maxLength; i++)
			{
				int v1 = i < parts1.Length ? int.Parse(parts1[i]) : 0;
				int v2 = i < parts2.Length ? int.Parse(parts2[i]) : 0;

				if (v1 > v2)
					return false ; 
				else if (v1 < v2)
					return true; 
			}
			return false; // version1 == version2
		}
	}

}
